package cricket_behaviour

import (
	"encoding/json"
	"longmen/server/config/global"
	"longmen/server/internal/services/nami/services/sports/cricket"
	"longmen/server/pkg/db/mysql/models/models_sports"
)

type (
	MatchApiResponse struct {
		Code  int `json:"code"`
		Query struct {
			Total int    `json:"total"`
			Type  string `json:"type"`
		} `json:"query"`
		Results struct {
			Match []Match `json:"match"`
			//Competition []struct {
			//	ID   int    `json:"id"`
			//	Name string `json:"name"`
			//	Logo string `json:"logo"`
			//} `json:"competition"`
			//Team []struct {
			//	ID   int    `json:"id"`
			//	Name string `json:"name"`
			//	Logo string `json:"logo"`
			//} `json:"team"`
		} `json:"results"`
	}
	Scores map[string][]int
	Match  struct {
		ID                 int   `json:"id"`                   // 比赛id
		UniqueTournamentID int   `json:"unique_tournament_id"` // 赛事id
		SeasonID           int   `json:"season_id"`            // 赛季id
		TournamentID       int   `json:"tournament_id"`
		VenueID            int   `json:"venue_id"`     // 场馆id
		HomeTeamID         int   `json:"home_team_id"` // 主队id
		AwayTeamID         int   `json:"away_team_id"` // 客队id
		StatusID           int32 `json:"status_id"`    // 比赛状态，详见状态码->比赛状态
		MatchTime          int64 `json:"match_time"`   // 比赛时间
		Neutral            int   `json:"neutral"`      // 是否中立场，1-是、0-否
		Coverage           struct {
			Mlive  int `json:"mlive"`  // 是否有动画，1-是、0-否
			Lineup int `json:"lineup"` // 是否有阵容，1-是、0-否
		} `json:"coverage"`                                         // 动画、阵容 字段
		Scores    Scores                            `json:"scores"` // 比分数据
		Weather   models_sports.CricketMatchWeather `json:"weather"`
		UpdatedAt int                               `json:"updated_at"`
	}
)

func (r MatchApiResponse) Interface() []cricket.InsertMatchInterface {
	a := make([]cricket.InsertMatchInterface, 0)
	for _, record := range r.Results.Match {
		a = append(a, record)
	}
	global.Log.Info("count : ", len(r.Results.Match))
	return a
}

func (c Match) Get() models_sports.CricketMatch {
	return models_sports.CricketMatch{
		ID:                 c.ID,
		UniqueTournamentID: c.UniqueTournamentID,
		SeasonID:           c.SeasonID,
		TournamentID:       c.TournamentID,
		VenueID:            c.VenueID,
		HomeTeamID:         c.HomeTeamID,
		AwayTeamID:         c.AwayTeamID,
		MatchTime:          c.MatchTime,
		Neutral:            c.Neutral,
		StatusID:           models_sports.CricketMatchStatus(c.StatusID),
		Mlive:              c.Coverage.Mlive,
		Lineup:             c.Coverage.Lineup,
		Scores:             c.Scores.String(),
		Weather:            c.Weather.String(),
		UpdatedAt:          c.UpdatedAt,
	}
}

func (s Scores) String() string {
	marshal, _ := json.Marshal(s)
	return string(marshal)
}
